package com.sixth.move.move_shopping_mall.controller;


import com.sixth.move.move_shopping_mall.domain.Employee;
import com.sixth.move.move_shopping_mall.domain.SearchEmployeeResponse;
import com.sixth.move.move_shopping_mall.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/employees")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String employeePage(Model model, Integer pageSize, Integer pageNum) {
        return "administration-pages/employee-list";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchEmployeeResponse searchEmployee(Integer pageNum, Integer pageSize){
        return employeeService.listEmployee(pageSize, pageNum);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addEmployeePage(Model model) {
        model.addAttribute("employee", new Employee());
        return "administration-pages/employee-add";
    }

    @GetMapping("/edit")
    public String editEmployeePage(@RequestParam("id") String id, Model model) {
        Employee employee = employeeService.findEmployeeByPrimaryKey(id);
        model.addAttribute("employee", employee);
        return "administration-pages/employee-add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String saveEmployee(Employee employee) {
        int updated = employeeService.saveEmployee(employee);
        return "redirect:/employees/search";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteEmployee(@RequestParam("id") String id) {
        int deleted = employeeService.deleteEmployeeByPrimaryKey(id);

        return deleted > 0;
    }

    public void string(){
        System.out.println("1");
    }
}
